package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.NewsEntity;
import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.service.NewsService;
import com.cari.oa.service.SysUserService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.Utils;

@Controller
@SessionAttributes("username")
public class NewsController {
	@Resource
	private NewsService newsService;
	
	@Resource
	private SysUserService sysUserService;
	
	public static final String MODULE_NAME = "NEWS_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
	
    @RequestMapping("/comprehensive/news/list.do")
    public String list(String title, Integer curPage, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = newsService.getCount(title);
        long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        
        List<NewsEntity> newsList = newsService.getPage(title, curPage, Contants.PAGE_SIZE);

        modelMap.addAttribute("newsList", newsList);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);

        modelMap.addAttribute("title", title);

        return "comprehensive/news/list";
    }
    
    @RequestMapping("/comprehensive/news/newslist.do")
    public String newslist(String title, Integer curPage, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = newsService.getCount(title);
        long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        
        List<NewsEntity> newsList = newsService.getPage(title, curPage, Contants.PAGE_SIZE);

        modelMap.addAttribute("newsList", newsList);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);

        modelMap.addAttribute("title", title);

        return "comprehensive/news/newslist";
    }
    
    @RequestMapping(value = "/comprehensive/news/insert.do", method = RequestMethod.GET)
    public String enterInsert(@ModelAttribute("username") String username, String qs, ModelMap modelMap) {
    	SysUserEntity user = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("qs", qs);
        return "comprehensive/news/insert";
    }

    @RequestMapping(value = "/comprehensive/news/view.do", method = RequestMethod.GET)
    public String enterView(@ModelAttribute("username") String username, String id, ModelMap modelMap) {
        NewsEntity news = newsService.getById(id);
        SysUserEntity user = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("news", news);
        return "comprehensive/news/view";
    }
    
    @RequestMapping(value = "/comprehensive/news/newsview.do", method = RequestMethod.GET)
    public String enterNewsView(@ModelAttribute("username") String username, String id, ModelMap modelMap) {
        NewsEntity news = newsService.getById(id);
        SysUserEntity user = sysUserService.getByEnname(username);
        modelMap.addAttribute("user", user);
        modelMap.addAttribute("news", news);
        return "comprehensive/news/newsview";
    }

    @RequestMapping(value = "/comprehensive/news/edit.do", method = RequestMethod.GET)
    public String enterEdit(String id, String qs, ModelMap modelMap) {
    	NewsEntity news = newsService.getById(id);
        modelMap.addAttribute("news", news);
        modelMap.addAttribute("qs", qs);
        return "comprehensive/news/edit";
    }
    
    @RequestMapping(value = "/comprehensive/news/insert.do", method = RequestMethod.POST)
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username, 
    		@Valid NewsEntity newsEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        if (newsService.existByTitle(newsEntity.getTitle()))
            return Utils.errorJsonResp("标题已存在");

        newsService.insert(newsEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增新闻" + newsEntity.getTitle());
        return Utils.normalJsonResp();
    }

    @RequestMapping(value = "/comprehensive/news/edit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postEdit(@ModelAttribute("username") String username, 
    		@Valid NewsEntity newsEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());

        newsService.update(newsEntity);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改新闻" + newsEntity.getTitle());
        return Utils.normalJsonResp();
    }
    
    @RequestMapping(value = "/comprehensive/news/delete.do")
    @ResponseBody
    public String postDelete(@ModelAttribute("username") String username, String id) {
    	NewsEntity newsEntity = newsService.getById(id);
        if(newsEntity == null) return Utils.errorJsonResp("该新闻已不存在，请刷新页面");

        newsService.deactivate(id);
        
        SysOperLogController.addOperLog(username, MODULE_NAME, "删除新闻" + newsEntity.getTitle());
        return Utils.normalJsonResp();
    }
}
